package com.cloudwise.monitoring_system.util;

import com.alibaba.fastjson.JSON;
import com.cloudwise.monitoring_system.entity.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.Date;
import java.util.UUID;

/**
 * @author Reisen
 * @description: kafka生产者
 * @date 2021/5/18 16:54
 */
@Slf4j
public class KafkaProducer {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    //发送消息方法
    public void send() {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        log.info("+++++++++++++++++++++  message = {}",  JSON.toJSONString(message));
        //topic-ideal为主题
        kafkaTemplate.send("test1", JSON.toJSONString(message));

    }
    public void send2() {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        log.info("+++++++++++++++++++++  message = {}",  JSON.toJSONString(message));
        //topic-ideal为主题
        kafkaTemplate.send("test2", JSON.toJSONString(message));

    }
}
